<template>
    <div class="xtx-category-page">
      <div class="container">
        <!-- 面包屑 -->
        <xtx-bread>
          <xtx-bread-item to="/">首页</xtx-bread-item>
          <!-- <xtx-bread-item>{{ $route.params.categoryname }}</xtx-bread-item> -->
          <!--transition内置动画标签在DOM发生变化是才会执行  -->
          <transition name="fade-right" mode="out-in">
            <!-- 添加key属性 强制让diff算法运行 强制更新DOM -->
            <!-- categoryname变化后diff算法比较新旧DOM 有key用key比较 无key用标签 -->
            <!-- 此时DOM就会更新 -->
            <xtx-bread-item :key="categoryname">{{ categoryname }}</xtx-bread-item>
          </transition>
        </xtx-bread>
        <!-- 轮播图 -->
        <xtx-carousel :sliders="list" style="height: 500px" />
          <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in secondList" :key="item.is">
            <a href="javascript:;">
              <img
                :src="item.picture"
              />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="obj in secondList" :key="obj.id">
        <div class="head">
          <h3>- {{ obj.name }} -</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <xtx-more />
        </div>
        <div class="body">
          <GoodsItem v-for="item in obj.goods" :key="item.id" :goods="item"></GoodsItem>
          <!-- <router-link to="/" class="goods-item" v-for="item in obj.goods" :key="item.id">
            <img
              :src="item.picture"
              alt=""
            />
            <p class="name ellipsis">{{ item.name }}</p>
            <p class="desc ellipsis">{{ item.desc }}</p>
            <p class="price">¥{{ item.price }}</p> -->
          <!-- </router-link> -->
        </div>
      </div>
      </div>
    </div>
  </template>

<script>
import { getBannerListAPI, getCategoryListAPI } from '@/api'
import { ref, onMounted } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import GoodsItem from '@/components/GoodsItem.vue'
export default {
  name: 'xtx-category-page',
  components: {
    GoodsItem
  },
  setup () {
    // 通过useRoute拿到route
    const route = useRoute()
    const list = ref([])
    const categoryname = ref('')
    // 二级列表
    const secondList = ref([])

    // 挂载后执行赋值即可实现刷新后面包屑动画
    onMounted(() => {
      categoryname.value = route.params.categoryname
    })
    // 方法1.利用计算属性解决setup只调用一次 面包屑不更新
    // const categoryname = computed(() => {
    //   return route.params.categoryname
    // })
    // -----------------------------------------------

    // 方法2.监听路由参数改变 to是下一个路由信息对象
    onBeforeRouteUpdate((to, from, next) => {
      categoryname.value = to.params.categoryname
      // 调用二级分类列表
      getCategoryList(to)
      // next() 允许跳转
      next()
    })

    onMounted(async () => {
      // 获取二级分类列表
      getCategoryList(route)
      // 商品页轮播图列表
      const res = await getBannerListAPI({ limit: 2 })
      list.value = res.result
    })

    // 获取二级分类列表
    const getCategoryList = async (route) => {
      const secondRes = await getCategoryListAPI({
        id: route.params.categoryid
      })
      secondList.value = secondRes.result.children
    }
    return {
      list,
      categoryname,
      secondList
    }
  }
}
</script>
<style scoped lang="less">
.xtx-category-page {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: var(--xtx-color);
          }
        }
      }
    }
  }
}
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
    .none {
      height: 220px;
      text-align: center;
      width: 100%;
      line-height: 220px;
      color: #999;
    }
  }
}
// 二级分类商品
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  background: #fff;
  transition: all 0.5s;
  &:hover {
    transform: translate3d(0, -3px, 0);
    box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
  }
  img {
    width: 160px;
    height: 160px;
  }
  p {
    padding-top: 10px;
  }
  .name {
    font-size: 16px;
  }
  .desc {
    color: #999;
    height: 29px;
  }
  .price {
    color: var(--price-color);
    font-size: 20px;
  }
}
</style>
